﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.BackgroundAudio;
using Common.Library;
using OnlineVideos.Game;
namespace OnlineVideos
{
    public partial class MemoryGame : UserControl
    {
        #region GlobalDeclaration


        #endregion

        #region Constructor

        public MemoryGame()
        {
            try
            {
                InitializeComponent();


                OnlineVideos.Game.Messenger.Default.Register<string>(this, (msg) =>
                {
                    if (OnlineVideos.Game.Constants.EndGameMessage == msg)
                    {
                        VisualStateManager.GoToState(this, EndState.Name, true);



                    }
                    if (BackgroundAudioPlayer.Instance.PlayerState != PlayState.Playing)
                    {
                        AudioTrack audioTrack = new AudioTrack();
                        audioTrack.BeginEdit();



                        audioTrack.Source = new Uri("Claps.mp3", UriKind.Relative);
                        audioTrack.Title = "";

                        audioTrack.Artist = "";
                        audioTrack.Album = "";
                        audioTrack.AlbumArt = null;
                        audioTrack.EndEdit();

                        BackgroundAudioPlayer.Instance.Track = audioTrack;
                        BackgroundAudioPlayer.Instance.Volume = 1;
                        BackgroundAudioPlayer.Instance.Play();

                    }


                });

                VisualStateGroup.CurrentStateChanged += new System.EventHandler<VisualStateChangedEventArgs>(VisualStateGroup_CurrentStateChanged);

                this.Loaded += (s, e) =>
                {
                    if (OnlineVideos.Game.MemoryViewModelLocator.MainStatic.MemoryCardList.Count > 0)
                    {
                        VisualStateManager.GoToState(this, "GameState", false);

                    }

                };
            }
            catch (Exception ex)
            {
                
                  Exceptions.SaveOrSendExceptions("Exception in MemoryGame Method In MemoryGame.cs file.", ex);
            }
        }

        #endregion

        #region Events

        void VisualStateGroup_CurrentStateChanged(object sender, VisualStateChangedEventArgs e)
        {
            currentState = e.NewState.Name;
            if (currentState == "GameState")
                if (BackgroundAudioPlayer.Instance.PlayerState == PlayState.Playing)
                {
                    if (BackgroundAudioPlayer.Instance != null && BackgroundAudioPlayer.Instance.Track.Source.ToString().Contains("Claps.mp3"))
                    {
                        BackgroundAudioPlayer.Instance.Stop();
                    }
                }

        }

        string currentState = "";

        #endregion

    }
}
